<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Week;

class WeekController extends Controller
{
    public function week_add(Request $request,Week $week){
        if($request->isMethod('post')){
            $data = $request->except('_token');
            if(empty($data['editorValue'])){
                return back()->withErrors(['请填写运程'])->withInput();
            }
            if(empty($data['date'])){
                return back()->withErrors(['请选择日期'])->withInput();
            }else{
                $week_err = $week->where('date',$data['date'])->get();
                if($week_err->count()){
                    return back()->withErrors(['该日期已被添加']);
                }
            }
           $data['yuncheng'] = $data['editorValue'];
            $result = $week->create($data);
            if($result != false && $result != null){
                session()->flash('success', '保存成功！');
                return redirect('admin/week/week_add');
            }else{
                session()->flash('success', '保存失败！');
                return redirect('admin/week/week_add');
            }
        }else{
            return view('Admin/week/week_add');
        }
    }
    //列表
    public function week_list(Week $week){
        $data = $week->get();
        return view('Admin/week/week_list',['data' => $data]);
    }
    //修改
    public function week_upd(Week $week,$id){
        $data = $week->where('id',$id)->get();
        return view('Admin/week/week_upd',['data' => $data]);
    }
    public function update(Request $request,Week $week){
        $data = $request->except('_token');
        if(!empty($data['editorValue'])){
            $re['content'] = $data['editorValue'];
        }
        if(empty($data['date'])){
            return back()->withErrors(['请选择日期']);
        }
        $re['date'] = $data['date'];
        $result = $week->where('id',$data['id'])->update($re);
        if($result != false){
            session()->flash('success','修改成功');
            return redirect()->back();
        }else{
            session()->flash('success','修改失败');
            return redirect()->back();
        }
    }
    //删除
    public function delete(Week $week,$id){
        $result = $week->where('id',$id)->delete();
        return redirect()->back();
    }
}
